/**
 * @copyright 2008 Daniel Fone
 */

Z = {
	basePath: '',
	
	init: function(){	
		// Route to the appropriate javascript
		var url = Z.parseURL(window.location.pathname);
		resourceJS = Z[url['resource']];
		if ( resourceJS && typeof resourceJS.init == 'function' ) resourceJS.init();
		
		// Other misc stuff
		Z.setupDelete();
		Z.setupConfirmDelete();
	},
	
	/**
	 * Javascript for each separate resource
	 */
	auth: {
		init: function(){
			this.selectLogonField();
		},
		
		selectLogonField: function(){
			$dbhost = $("input[@name='dbhost']");
			$dbuser = $("input[@name='dbuser']");
			if ( $dbhost.val() == '' ){
				$dbhost.select();
			} else {
				$dbuser.select();
			}
		}
	},
	
	setupConfirmDelete: function(){
		$("form").submit(function(){
			if ( this.action.indexOf('delete') != -1 )
				return confirm('Are you sure you want to delete this?');
		});
	},
	
	setupDelete: function(){
		$(":input.delete").change(function(){
			if ( this.checked )	$(this).parents('tr').addClass('toDelete').find(':input').not(this).attr('disabled', 'disabled');
			else $(this).parents('tr').removeClass('toDelete').find(':input').not(this).removeAttr('disabled');
		}).hide();
	},
	
	/**
	 * Some random helper methods
	 */
	parseURL: function(url){
		var chunks = url.replace(Z.basePath, '').split('/');
		return {
			resource:	chunks.length > 1 ? chunks[1] : null,
			action:		chunks.length > 2 ? chunks[2] : null,
			id:			chunks.length > 3 ? chunks[3] : null
		};
	}
};

$(Z.init);
